from keras.layers import Dense, Activation, Flatten, Dropout
from keras.models import Sequential
import numpy as np
import random
import cv2
import os
from GeneralKit import *

def startBlueTrain(source, modelName):
	sourcedir = source
	indexnum = 30
	input_shape = (12,3)
	print("start")

	model = Sequential()
	model.add(Dense(100, init='uniform', input_shape=input_shape))
	model.add(Activation('relu'))
	model.add(Dense(50))
	model.add(Activation('relu'))
	model.add(Flatten())
	model.add(Dropout(0.3))
	model.add(Dense(indexnum))
	model.add(Activation('softmax'))

	model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
	dirs = os.listdir(sourcedir)

	random.shuffle(dirs)

	datas = []
	labels = []
	index = 0

	print("载入蓝牙数据样本")
	for f in dirs:	
		blues = np.loadtxt(sourcedir + f)
		#random.shuffle(blues)
		label = int(f.split("_")[0])
		label = getLabel(label)
		data = []
		for i in range(len(blues)):
			print(f,i)
			templeBlue = tuple(blues[i])
			data.append(templeBlue)
			index += 1
			print('\r', index, end='')
		datas.append(data)
		labels.append(label)

	datas = np.array(datas)
	labels = np.array(labels)

	model.fit(datas, labels, epochs=2, batch_size=5)
	model.save(modelName)

	print('finished')

if __name__=='__main__':
	startBlueTrain('blue_data/','blueMod.h5')